package unimelb.filesync.client.protocol;

import java.net.Socket;

import unimelb.filesync.common.misc.ProtocolContext;
import unimelb.filesync.common.network.NetworkConnection;
import unimelb.filesync.common.protocol.IFileSyncPhase;

public class ConnectToServerPhase implements IFileSyncPhase {

    private final ProtocolContext context;

    public ConnectToServerPhase(ProtocolContext context) {
        super();
        this.context = context;
    }

    @Override
    public void execute() {
        try {
            final String hostName = context.getObject(ProtocolContext.HOST);
            final Integer serverPort = context.getObject(ProtocolContext.PORT);
            final Socket hotLineSocket = new Socket(hostName, serverPort);

            context.addObject(ProtocolContext.HOT_LINE_CONNECTION, new NetworkConnection(hotLineSocket));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
